<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.util.Random" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post">
    <%
        String inum1 = "";
        String inum2 = "";
        char op = '+';
        String str = "";
        int i = 0;
        char[] sop = new char[]{'+', '-', '*', '/'};
        if (request.getParameter("judge1") != null && request.getParameter("judge1").equals("1") && request.getParameter("judge2").equals("0")) {
            System.out.println(request.getParameter("chuti"));
            Random random = new Random();
            inum1 = random.nextInt(20) + "";
            inum2 = (random.nextInt(20) + 1) + "";
            int opNum = random.nextInt(4);
            op = sop[opNum];
            i = 1;//出题了就可以评分
        }
        if (request.getParameter("judge2") != null && request.getParameter("judge2").equals("1")) {
            String userResult = request.getParameter("userNum");
            String s1 = request.getParameter("inum1");
            String s2 = request.getParameter("inum2");
            String sop1 = request.getParameter("op");
            int sum = 0;
            switch (sop1.charAt(0)) {
                case '+':
                    sum = Integer.parseInt(s1) + Integer.parseInt(s2);
                    break;
                case '-':
                    sum = Integer.parseInt(s1) - Integer.parseInt(s2);
                    break;
                case '*':
                    sum = Integer.parseInt(s1) * Integer.parseInt(s2);
                    break;
                case '/':
                    sum = Integer.parseInt(s1) / Integer.parseInt(s2);
                    break;
            }
            if (userResult == null) userResult = "";
            if (!userResult.equals("")) {
                int userInput = Integer.parseInt(userResult);
                if (userInput == sum)
                    str = "正确";
                else
                    str = "错误";
            }
        }
    %>
    <%=inum1%>
    <%=op%>
    <%=inum2%>
    <%="="%>
    <input name="inum1" type="hidden" value="<%=inum1%>">
    <input name="op" type="hidden" value="<%=op%>">
    <input name="inum2" type="hidden" value="<%=inum2%>">
    <input name="judge1" type="hidden" value="1">
    <input name="judge2" type="hidden" value="<%=i%>">
    <input name="userNum" type="text"><br/>
    <input name="score" type="submit" value="评分">
    <input name="chuti" type="submit" value="出题">
    <%=str%>

</form>
</body>
</html>
